import React from 'react';
import { Form, Input, Button, message, Modal, Space, Tabs, InputNumber } from 'antd';
import { ExclamationCircleOutlined } from '@ant-design/icons';
import { connect } from 'umi';
// import UploadUtil from '@/components/UploadUtil';

const currControl = 'Edu_Answer';
const { confirm } = Modal;
const { TabPane } = Tabs;

class VaccinesEdit extends React.Component<any, any> {
  constructor(props: any) {
    super(props);
    this.state = {
      loading: false,
      datas: {},
      imageUrl: '',
      imgList: [],
    };
  }
  formRef: any = React.createRef();
  componentDidMount() {
    const { ParentData, QDId } = this.props;
    if (ParentData && ParentData.id) {
      this.getData(ParentData.id);
    } else {
      var model = {
        questionId: QDId,
      };
      this.formRef.current.setFieldsValue({ model });
    }
  }
  getData = (id: number) => {
    const { dispatch } = this.props;
    if (id) {
      dispatch({
        type: 'Abp/get',
        payload: { id },
        control: currControl,
      }).then(() => {
        var result = this.props.response.result;
        if (result.image) {
          this.setState({ imageUrl: result.image });
        }
        this.formRef.current.setFieldsValue({ model: result });
      });
    }
  };
  onFinish = async (values: any) => {
    var act = 'Abp/create';
    if (values.model.id) {
      act = 'Abp/update';
    }
    if (this.state.imageUrl) {
      values.model.image = this.state.imageUrl;
    }
    this.props
      .dispatch({
        type: act,
        payload: values.model,
        control: currControl,
      })
      .then(() => {
        var resp = this.props.response;
        if (resp.success) {
          message.success('保存成功！');
          this.props.fetchDatas(this.props.QDId);
        } else {
          message.error('保存错误，请联系管理员');
        }
      });
  };
  ResetForm = () => {
    var data = {
      id: 0,
    };
    this.setState({ datas: data });
    this.formRef.current.resetFields();
  };
  handleDel = () => {
    this.props
      .dispatch({
        type: 'Abp/delete',
        payload: { Id: this.state.datas.id },
        control: currControl,
      })
      .then(() => {
        var resp = this.props.response;
        if (resp.success) {
          message.success('删除成功！');
          this.props.fetchDatas(this.props.ParentData.questionId);
          this.formRef.current.resetFields();
        } else {
          message.error('删除错误，请联系管理员！');
        }
      });
  };
  showConfirm = () => {
    var _this = this;
    confirm({
      title: '警告',
      icon: <ExclamationCircleOutlined />,
      content: '删除后将不可恢复，确定删除？',
      onOk() {
        _this.handleDel();
      },
      onCancel() {},
    });
  };
  handleChange = (editorState: any) => {
    this.setState({
      contentEditor: editorState.toHTML(),
    });
  };
  onChange = (value: number[]) => {
    this.setState({
      inputValue: value,
    });
  };
  handleUpload = (info: any) => {
    if (info.file.status === 'done' && info.file.response) {
      this.setState({
        imageUrl: info.file.response.result,
      });
    }
  };
  render() {
    const { datas, loading } = this.state;
    return (
      <div>
        <Form ref={this.formRef} layout="vertical" onFinish={this.onFinish}>
          <Tabs defaultActiveKey="1">
            <TabPane tab="答案编辑" key="1">
              <div style={{ display: 'none' }}>
                <Form.Item label="id" name={['model', 'id']}>
                  <Input />
                </Form.Item>
                <Form.Item name={['model', 'questionId']} rules={[{ required: true }]}>
                  <Input />
                </Form.Item>
              </div>
              <Form.Item
                name={['model', 'answerNum']}
                label="答案编号"
                rules={[{ required: true }]}
              >
                <Input />
              </Form.Item>
              <Form.Item name={['model', 'orderId']} label="排序号" rules={[{ required: true }]}>
                <InputNumber />
              </Form.Item>
              <Form.Item
                name={['model', 'ansContent']}
                label="答案内容"
                rules={[{ required: true }]}
              >
                <Input.TextArea rows={3} />
              </Form.Item>
              <Form.Item name={['model', 'selectAdv']} label="选择建议">
                <Input.TextArea rows={3} />
              </Form.Item>
              <Form.Item name={['model', 'unSelectAdv']} label="未选择建议">
                <Input.TextArea rows={3} />
              </Form.Item>
              <Form.Item name={['model', 'content']} label="报告内容">
                <Input.TextArea rows={3} />
              </Form.Item>
              <Form.Item name={['model', 'points']} label="分数">
                <InputNumber step={0.5} />
              </Form.Item>
            </TabPane>
          </Tabs>
          <Form.Item>
            <Space>
              <Button type="primary" htmlType="submit" loading={loading}>
                确定
              </Button>
              <Button onClick={this.ResetForm}>重设</Button>
              <Button danger onClick={this.showConfirm} disabled={!datas.id}>
                删除
              </Button>
            </Space>
          </Form.Item>
        </Form>
      </div>
    );
  }
}
export default connect(({ Abp, Category }: API.IConnectState) => ({
  response: Abp.response,
  currentUser: Abp.currentUser,
  Category,
}))(VaccinesEdit);
